Wrapper Class
Wrapper class allows you to convert the object into primitive and vice versa. After the J2SE 5.0, unboxing and autoboxing are the two features that allow this conversion automatically. Autoboxing is the process of an automatic conversion from primitive to object and unboxing is the process of converting an object to primitive.
Where to use-
As java is based on objects and we use an object in collections, synchronization, serialization etc. below are the scenarios where the wrapper class can be used-
- Call by value is supported by Java if you are going to pass a primitive value the original value will not be changed. But the original value will be changed if we will convert the primitive value to an object.
- To perform serialization the objects need to be converted to the stream. With the help of the wrapper class, we can easily convert the primitive value to objects easily.
- You can use java.util.package for the utility classes to work with the objects.
- Java collection framework work deals only with the object.
Below are eight wrapper classes of java.lang package-
Primitive Type |
Wrapper Class |
boolean |
boolean |
char |
character |
byte |
byte |
short |
short |
Int |
Int |
long |
long |
float |
float |
double |
double |
1. Boolean class- this class will wrap primitive type boolean value into an object. Below are the methods that are used within the Boolean class.
- booleanValue() - it will return a Boolean primitive for the value of the Boolean object.
- compare()- it will be used to compare the two Boolean values.
- compareTo()- it will be used to compare this Boolean instance with another.
- equals()- it will return a Boolean value as true if the provided argument is a Boolean object that represents the same value as this object.
- getBoolean() - it will return a Boolean value as true if the system property name is the same as the string "true".
- hashCode()- it will return a hash code for the Boolean object.
- logicalAnd()- it will return the result of logical AND operation that is performed on the assigned boolean operands.
- logicalOr()- it will return the result of logical OR operation that is performed on the assigned boolean operands.
- logicalXor()- it will return the result of logical XOR operation that is performed on the assigned boolean operands.
- parseBoolean()- it will allow you to parse the string argument as a Boolean.
- toString()- it will return a String instance that represents the specified Boolean's value or the specified boolean.
- valueOf()- it will return a Boolean instance that represents the specified Boolean value or string value.
2. Character class- this class will allow you to wrap the char primitive type into an object. The object of character type will contain a single char field. You can use the Unicode data to specify the data members of the class Character. Below are used methods within the class Character-
- charCount(int codePoint)- it is used to determine the char values which will represent the given character.
- charValue()- it will return the value of the provided Character object.
- codePointAt(char[]a, int index)- it will return the codePoint for the provided index for the given array.
- codePointAt(char[]a, int index, int limit )- it will return the codePoint of the char array at the given index where you can use the elements of the array with the index less than the specified limit.
- codePointAt(CharSequence seq, int index)- it will return the codePoint at the given index for the provided CharSequence.
- codePointBefore(char[]a, int index)- it will return the codePoint for the given array with the preceding index.
- codePointBefore(char[]a, int index, int start)- it will return the codePoint for the provided array in the preceding index where you can use the array elements having index greater than or equal to start.
- codePointBefore(CharSequence, int index)- it will return the CodePoint for the provided CharSequence using the preceding index.
- codePointCount(char[]a, int offset, int count)- the total number of Unicode codePoints in the provided subarray will be returned of the char array argument.
- codePointCount(CharSequence seq, int beginIndex, int endIndex)- it will return the number of Unicode codePoints that is provided in the specified text range for the given char sequence.
- codePointOf(String name)- it will return the codePoint value for the provided Unicode character that is specified by a particular character name.
- compare(char x, char y)- it will allow you to compare two character type values on a numeric basis.
- compareTo(Character anotherCharacter)- it will allow you to compare the two Character type objects on numeric basis.
- digit(char ch, int radix)- it will return the numeric value of the provided character in the specified index.
- digit(int codePoint, int radix)- it will return the numeric value of the specified character in the specified radix.
- equals(Object obj)- it will allow you to compare the provided object with the specified object.
- forDigit(int digit, int radix)- it will allow you to determine the presence of the character for a particular digit in the given radix.
- getDirectionality(char ch)- it will return the Unicode directionality property for the given character.
- getDirectionality(int codePoint)- it will return the Unicode directionality property for the given codePoint.
- getName(int codePoint)- it will return the Unicode name for the provided codePoint or may return null if the codePoint is not assigned.
- getNumericValue(char ch)- it will return the integer type value for the given Unicode character.
- getNumericValue(int codePoint)- it will return the integer type value for the given Unicode codePoint.
- getType(char ch)- it will return the value which will provide you with the general category of the character.
- getType(int codePoint)- it will return the value which will specify the general category of the character.
- hashcode()- it will return the hash code for the given character.
- hashcode(char value)- it will return the hash code for a given char value.
- isAlphabetic(int codePoint)- it will allow you to determine whether the provided character is an alphabet or not.
- isBmpCodePoint(int codePoint)- it will allow you to determine whether the specified character is in the Basic Multilingual Plane or not.
- isDefined(char ch)- it will allow you to determine whether the character is Unicode defined or not.
- isDefined(int codePoint)- it will allow you to determine whether the Unicode code point is Unicode defined or not.
- isDigit(char ch)- it will allow you to determine whether the provided character is a digit or not.
- isDigit(int codePoint)- it will allow you to determine whether the given Unicode code point is a digit or not.
- isHighSurrogate(char ch)- it will allow you to determine whether the given char values is a Unicode high-surrogate code point or not.
- isIndentifierIgnorable(char ch)- it will allow you to determine whether the provided character is an ignorable character in the Java identifier or not.
- isIndentifierIgnorable(int codePoint)- it will allow you to determine whether the provided Unicode codePoint is an ignorable character in the Java identifier or not.
- isIdeographic(int codePoint)- it will allow you to determine whether the provided character is a CJKV(Chinese, Japanese, Korean and Vietnamese) ideograph or not.
- isISOControl(char ch)- it will allow you to determine whether the provided character is an ISO Control character or not.
- isISOControl(int codePoint)- it will allow you to determine whether the provided Unicode code point is an ISO Control character or not.
- isJavaIdentifierPart(char ch)- it will allow you to determine whether the provided character is a part of the Java identifier or not.
- isJavaIdentifierPart(int codePoint)- it will allow you to determine whether the provided Unicode code point is a part of the Java identifier or not.
- isJavaIdentifierStart(char ch)- it will not allow you to determine whether the provided character is accepted as the first character in the Java identifier or not.
- isJavaIdentifierStart(int codePoint)- it will allow you to determine whether the given Unicode code point is accepted as the first character in the Java identifier or not.
- isJavaLetter(char ch) this function is now replaced by the isJavaIdentifierStart(char).
- isJavaLetterOrDigit(int codePoint)- this function is now replaced by the isJavaIdentifierPart(char).
- isLetter(char ch)- it will allow you to determine whether the given character is a letter or not.
- isLetter(int codePoint)- it will allow you to determine whether the given Unicode code point is a letter or not.
- isLetterOrDigit(char ch)- it will allow you to determine whether the given character is a letter or digit or not.
- isLetterOrDigit(int codePoint)- it will allow you to determine whether the provided Unicode code point is a letter or digit or not.
- isLowerCase(char ch)- it will allow you to determine whether the specified character is a lowercase character or not.
- isLowerCase(int codePoint)- it will allow you to determine whether the given Unicode code point is a lowercase character or not.
- isLowSurrogate(char ch)- it will allow you to determine whether the specified char value is a Unicode low-surrogate code unit or not.
- isMirrored(char ch)- it will allow you to determine whether the given character is mirrored or not.
- isMirrored(int codePoint)- it will allow you to determine whether the Unicode code point is mirrored or not.
- isSpace(char ch)- this method is now replaced by isWhiteSpace(char ch) method.
- isSpaceChar(char ch)- it will allow you to determine whether the given character is a Unicode space character or not.
- isSupplementaryCodePoint(int codePoint)- it will allow you to determine whether the provided Unicode code point is in the range of supplementary characters or not.
- isSurrogate(char ch)- it will allow you to determine whether the provided char value is a Unicode surrogate code unit.
- isSurrogatePair(char high, char low)- it will allow you to determine whether the given char value pair is a valid Unicode surrogate pair or not.
- isTitleCase(char ch)- it will allow you to determine whether the given character is a title case character or not.
- isTitleCase(int codePoint)- it will allow you to determine whether the provided Unicode code point is a titlecase character or not.
- isUnicodeIdentifierPart(char ch)- it will allow you to determine whether the provided character is a part of a Unicode identifier or not?
- isUnicodeIdentifierPart(int codePoint)- it will allow you to determine whether the provided Unicode code point is a part of Unicode identifier or not.
- isUnicodeIdentifierStart(char ch)- it will allow you to determine whether the specified character is accepted as the first character in the Unicode identifier or not.
- isUnicodeIdentifierStart(int codePoint)- it will allow you to determine whether the given Unicode code point is accepted as the first character in the Unicode identifier or not.
- isUpperCase(char ch) -it will allow you to determine whether the specified character is an uppercase character or not.
- isUpperCase(int codePoint)- it will allow you to determine whether the provided Unicode code point is an uppercase character or not.
- isValidCodePoint(int codePoint)- it will allow you to determine whether the provided character is a valid Unicode code point value or not.
- isWhitespace(char ch)- it will allow you to determine whether the provided character is a white space or not.
- isWhitespace(int codePoint)- it will allow you to determine whether the provided Unicode code point is a white space or not.
- lowSurrogate(int codePoint)- it will return the low surrogate code unit )of the surrogate pair which specifies the supplementary character in the UTF-16 encoding.
- offsetByCodePoints(char[]a, int start, int count, int index, int codePointOffset)- it will return the given char subarray index which is given index offset by the codePointOffset code points.
- offsetByCodePoints(CharSequence seq, int index, int codePointOffset)- it will return the given char subarray index which is given index offset by the codePointOffset code points.
- reverseBytes(char ch)- it will return the obtained value in reverse order of the given bytes in the specified char values.
- toChars(int codePoint)- it will convert the specified Unicode code point into its UTF-16 specification which is stored in a char array.
- toChars(int codePoint, char[] dst, int dstIndex)- it will convert the specified Unicode code point into its UTF-16 specification.
- toCodePoint(char high, char low)- it will convert the provided surrogate pair to its corresponding supplementary code point value.
- toLowerCase(char ch)- it will convert the character type argument to lowercase using case mapping.
- toLowerCase(int codePoint)- it will convert the character type argument(Unicode code point) to lowercase using case mapping.
- toString()- it will return a string type object which represents a character value.
- toString(char ch)- it will return a string type object which defines the specified character
- toTitleCase(char ch)- it will convert the character type argument to titlecase using case mapping.
- toTitleCase(int codePoint)- it will convert the character type argument(Unicode code point) to lowercase using case mapping information.
- toUpperCase(char ch)- it will convert the character type argument to uppercase using case mapping information.
- toUpperCase(int codePoint)- it will convert the character type argument(Unicode code point) to uppercase using case mapping information.
- valueOf(char c)- it will return the Character instance which determines the specified character value.
Example-
import java.util.Scanner;
public class Simple {
public static void main(String[] args) {
System.out.print("Enter the first input:");
Scanner scanner = new Scanner(System.in);
char[] value1 = scanner.nextLine().toCharArray();
int result1 = 0;
for (char ch1 : value1) {
result1 = Character.charCount(ch1);
}
System.out.print("The value comes to: "+result1+"\n");
System.out.print("Enter the second input:");
char[] value3 = scanner.nextLine().toCharArray();
for (char ch3 : value3) {
boolean result3 = Character.isDigit(ch3);
if(result3){
System.out.println("The character '" + ch3 + "' is a digit. ");
}
else{
System.out.println("The character '" + ch3 + "' is not a digit.");
}
System.out.print("Enter the fourth input:");
char[] value4 = scanner.nextLine().toCharArray();
for (char ch4 : value4) {
boolean result4 = Character.isISOControl(ch4);
System.out.println("The fourth character '"+ch4+"' is an ISO Control:"+result4);
}
}
char c1, c2, c3, c4;
c1 = 'T';
c2 = 'k';
c3 = '\n';
c4 = 'G';
boolean b1, b2, b3,b4;
b1 = Character.isLetter(c1);
b2 = Character.isLowerCase(c2);
// b3 = Character.isSpace(c3);
b4 = Character.isDefined(c4);
System.out.println(b1);
System.out.println(b2);
//System.out.println(b3);
System.out.println(b4);
}
}
Output-
3. Byte class- this class will allow you to wrap the primitive byte type into an object. Its object will contain byte type field. Below is the list of the methods under the byte class that you can use.
- byteValue()- It will return this Byte value as a byte
- compare()- It will compare the two provided byte values
- compareTo()- It will compare the two Byte objects on a numeric basis
- compareUnsigned()- It will compare the two unsigned byte values
- decode()- It will allow you to decode a String into a Byte
- doubleValue()- It will return a double value for provided Byte object
- equals()- It will allow you to compare this object with the specified byte object
- floatValue()- It will return the value of this Byte as a Float
- hashCode()- It returns a hash code for this Byte object
- intValue()- It will return an int value corresponding this Byte object
- longValue()- It will return a long value for this corresponding Byte object
- parseByte()- It will parse the string argument as a signed decimal byte.
- shortValue()- It will return a short value for this corresponding Byte object
- toString()- It will return a string representation of the Byte object
- toUnsignedInt()- It will be used to convert the specified byte to an int by an unsigned conversion
- toUnsignedLong()- It will allow you to convert the specified byte to a long by an unsigned conversion
- valueOf()- it will return a Byte instance that represents the specified byte or string value.
Example-
public class Simple{
static int var=1;
public static void main(String[] args) {
Byte b1= 5 ;
Byte b2= 67 ;
int val = Byte.compare(b1,b2);
if (val>0) {
System.out.println(var++ + ". "+b1 + " is greater than " + b2);
}
else{
System.out.println(var++ + ". "+b2 + " is greater than " + b1);
}
int f1 = b1.hashCode();
System.out.println(var++ + ". "+"Hash code value of "+b1+ " is : "+f1);
Float f2 = b2.floatValue();
System.out.println(var++ + ". "+"Float value of "+b2+ " is : "+f2);
Integer f3 = b2.intValue();
System.out.println(var++ + ". "+"Integer value of "+b2+ " is : "+f3);
String str="123";
Byte f4 = Byte.decode(str);
System.out.println(var++ + ". "+"Decoded value of "+str+ " is : "+f4);
}
}
Output-
4. Short class- this class will allow you to wrap a primitive short type value in an object. The object of this class will contain a short type of field. Below is the list of the methods that are defined within the short class.
- byteValue()- It will return this Short value as a short
- compare()- It will compare the two specified short values
- compareTo()- It will compare the two Short objects on a numeric basis
- compareUnsigned()- It will compare the values of the two unsigned short
- decode()- It will allow you to decode a String into a Short
- doubleValue()- It will return a double value for this Short object
- equals()- It will allow you to compare this object with the specified short object
- floatValue()- It will return this Short value as a Float
- hashCode()- It will return a hash code for this Short object
- intValue()- It will return an int value for this Short object
- longValue()- It will return a long value for this Short object
- parseShort()- It will allow you to parse the string argument as a signed decimal short.
- reverseBytes()- It will return the fetched value in reverse order of the bytes in 2's complement representation of the given short value.
- shortValue()- It will return a short value for this Short object
- toString()- This method will return a string representation for the Short's object
- toUnsignedInt()- It will allow you to convert the provided short to an int by an unsigned conversion
- toUnsignedLong()- It will allow you to convert the provided short to a long by an unsigned conversion
- valueOf()- it will return a Short instance that represents the specified short or string value.
Example-
public class Simple {
static int var=1;
public static void main(String[] args) {
Short s1 = 50 ;
Short s2 = 120 ;
Short s3=12;
int val = s1.compareTo(s2);
if (val>0) {
System.out.println(var++ + ". "+s1 + " is greater than " + s2);
}
else{
System.out.println(var++ + ". "+s2 + " is greater than " + s1);
}
Boolean b1 = s3.equals(s2);
if (b1) {
System.out.println(var++ + ". "+s2 + " and " + s3 +" are equal.");
}
else{
System.out.println(var++ + ". "+s1 + " and " + s2 +" are not equal.");
}
Long f3 = s2.longValue();
System.out.println(var++ + ". "+"Long value of "+s2+ " is : "+f3);
String f4 = s2.toString();
System.out.println(var++ + ". "+"String value of "+s2+ " is : "+f4);
Double f5 = s1.doubleValue();
System.out.println(var++ + ". "+"Double value of "+s1+ " is : "+f5);
}
}
Output-
5. Integer class- Java.lang.Number package contains the integer class. This class will allow you to wrap the primitive integer type into an object. The object of the class will contain an integer type field only. Below is the list of methods that are defined within the integer class.
- bitCount()- It will return the number of 1-bits in the 2's complement binary representation for the specified int value.
- byteValue()- It will allow you to convert the given number into a primitive byte type and returns the integer value object as a byte.
- compare()- It will allow you to compare the two int values numerically and returns its integer equivalent.
- compareTo()- It will allow you to compare the two integer objects numerically and returns the output as -1, 0 or 1.
- compareUnsigned()- It will compare the two integer objects numerically that will treat the values as unsigned and returns s -1, 0 or 1.
- decode()- It will allow you to decode a String object into an integer value.
- divideUnsigned()- It will return the unsigned quotient of dividing the first argument by the second argument.
- doubleValue()- It will convert the provided Integer value and returns a double equivalent.
- equals()- It will allow you to compare the parameter value to current Integer object value and returns a boolean value.
- floatValue()- It converts the given Integer value and returns the result as a float equivalent.
- getInteger()- It will allow you to determine the integer value of the system property provided with the specified name.
- hashCode()- It will return a hash code for the given Integer.
- highestOneBit()- It will return an int value with at most a single one-bit, in the position of the highest-order.
- lowestOneBit()- It will return an int value with at most a single one-bit, in the position of the lowest-order.
- max()- It will return the maximum value amongst the two arguments.
- min()- It will return the minimum value amongst the two arguments.
- intValue()- It will return the specified value number as an int.
- longValue()- It will return the specified value of long object as long equivalent.
- numberOfLeadingZeros()- It will return the number of zero bits preceding the highest-order ("leftmost") one-bit in the 2's complement binary representation of the specified int value.
- numberOfTrailingZeros()- It will return the number of zero bits following the lowest-order ("rightmost") one-bit in the 2's complement binary representation of the specified int value.
- parseInt()- It will allow you to parse the string argument as a signed decimal Integer object.
- parseUnsignedInt()- It will parse the String argument as an unsigned decimal Integer object.
- remainderUnsigned()- It will return the unsigned remainder by dividing the first argument by the second argument where every argument and the result is used as an unsigned value.
- reverse()- It will return the reverse order values of the bits in the 2's complement binary representation of the provided int value.
- reverseBytes() - It will return the value in reverse order of the bytes in the 2's complement binary representation of the specified int value.
- rotateLeft()- It will return the value obtained by rotating the 2's complement binary representation of the provided int value left by the specified number of bits.
- rotateRight()- It will return the value obtained by rotating the 2's complement binary representation of the provided int value right by the specified number of bits.
- shortValue()- It will return this Integer value as a short type after a primitive conversion.
- signum()- It will return the signum function of the provided int value.
- sum()- It will return the sum by adding two integers together using the + operator.
- toBinaryString()- It will return a string representation of the integer argument as an unsigned integer in binary base 2.
- toHexString()- It will return a string representation of the integer argument as an unsigned integer in binary base 16.
- toOctalString()- It will return a string representation of the integer argument as an unsigned integer in binary base 8.
- toString()- It will return a String object that represents the Number Object value.
- toUnsignedString()- It will allow you to convert the argument to a long by an unsigned conversion.
- toUnsignedLong()- It will return a string representation of the provided argument as an unsigned decimal value.
- valueOf()- It will return the corresponding Integer Object that holds the value of the argument passed.
6. Long class- this class will allow you to wrap the primitive long type into an object. The object of the class will contain the long type of field only. Below is the list of the methods used within the long class.
- bitCount(long i)- It will return the one bits number in two’s complement binary representation.
- byteValue()- It will return the long value as a byte after the conversion.
- compare(long x, long y)- It will compare the two long values.
- compareTo(long another long)- It will compare the two long objects on a numeric basis.
- compareUnsigned(long x, long y)- It will compare the two long values maintaining the values unsigned.
- decode(String nm)- It will be used to decode a string into Long.
- divideUnsigned(long dividend, long divisor)- It will return the unsigned quotient by dividing the first argument by the second.
- doubleValue()- It will return the Long value as a double after the conversion.
- equals(Object obj)- It will compare the object with the specified object.
- floatValue()- It will return the float type value for the specified Long object.
- getLong(String nm)- It will determine the long value for the provided name.
- getLong(String nm, Long val)- It will return the long value for the provided name.
- hashCode()- It returns the hash code for the specified Long object.
- hashCode(Long value)- It will return the hash code for the provided Long value.
- highestOneBit(long i)- It will return a long value with at most one single bit in the highest order.
- intValue()- It will return the Long value as an int after the conversion.
- longValue()- It will return the Long value as a long after the conversion.
- lowestOneBit(long i)- It will return a long value with at most one single bit in the lowest order.
- max(long a, long b)- It will return the greater of the two provided long values.
- min(long a, long b)- It will return the smaller of the two provided long values.
- numberOfLeadingZeroes(long i)- It will return the total number of zero bytes in the highest order.
- numberOfTrailingZeroes(long i)- It will return the total number of zero bytes in the lowest order.
- parseLong(CharSequence s, int beginIndex, int endIndex, int radix)- It will allow you to parse the CharSequence argument with a specified long value in the given index
- parseLong(String s)- It will allow you to parse the string parameter as a signed decimal long.
- parseLong(String s, int radix)- It will parse the string argument as a signed long value as given in the radix.
- parseUnsignedLong(CharSequence s, int beginIndex, int ndIndex, int radix)- It will allow you to parse the given CharSequence argument as an unsigned long in the given radix.
- parseUnsignedLong(String s)- It will allow you to parse the given string argument as an unsigned long.
- parseUnsignedLong(String s, int radix)- It will allow you to parse the provided string argument as an unsigned long value in the index that is specified by the second argument.
- remainderUnsigned(long dividend, long divisor)- It will return the unsigned remainder by dividing the first argument with the second argument.
- reverse(long i)- It will return the value in the reverse order of bits in 2’s complement binary representation.
- reverseBytes(long i)- It will return the value in reverse order of bits in 2’s complement representation.
- rotateLeft(long i, int distance)- It will return the value in the rotation of the 2’s complement binary representation of the given long value by left and by the specified number of bits.
- rotateRight(long i, int distance)- It will return the value in the rotation of the 2?s complement binary representation of the given long value by right and by the specified number of bits.
- shortValue()- It will return the short type value for the given Long object.
- signum(long i)-It will return the signum function for the given long value.
- sum(long a, long b)- It will add two long values as per the + operator.
- toBinaryString(long i)- It will return the string representation for the given long argument as an unsigned argument with base 2.
- toHexString(long i)- It will return the string representation for the given long argument as an unsigned argument with base 16.
- toOctalString(long i)- It will return the string representation for the given long argument as an unsigned argument with base 8.
- toString()- It will return the string which will represent the long value.
- toString(long i)- It will return the string which will represent the specified long.
- toString(long i, int radix)- It will return the string representation for the first argument which is specified by the second argument.
- toUnsignedString(long i)- It will return the string representation for the argument as an unsigned decimal value.
- toUnsignedString(long i, int radix)- It will return the string representation for the first argument as an unsigned integer value in the radix.
- valueOf(l)- It will return a long instance which specifies the specified value.
- valueOf(String s)- It will return a long instance which will hold the value of the specified string.
- valueOf(String s int radix)- It will return the Long object which will hold the value of specified string when parsed with the radix provided by the second argument
Example-
public class Simple {
public static void main(String[] args) {
Long x1=992l;
Long x2=88l;
Long x3=8l;
int b1 = Long.compare(x1, x2);
if(b1==0)
{
System.out.println("Both '"+x1+"' and '"+ x2+"' are same");
}
else if(b1>0)
{
System.out.println(x1+" is greater than "+x2);
}
else
{
System.out.println(x1+" is smaller than "+x2);
}
System.out.println("The hashcode for the value '"+x1+"' is given as :"+x1.hashCode());
short b2 = x3.shortValue();
System.out.println("The short value for '"+x2+"' is given as : "+b2);
}
}
Output-
7. Float class - this class will allow you to wrap the primitive float type into an object. The class object will only contain the float type filed. Below is the list of the methods that are defined within the float class.
- byteValue()- it will return the value of this float as a byte
- compare()- it will allow you to compare the two specified float values
- compareTo()- it will compare the two float objects on a numeric basis
- doubleValue()- it will return a double value for this Float object
- equals()- it will compare the object with the specified object
- floatToIntBits()- it will return a floating-point representation value according to IEEE floating-point "single format " bit layout
- floatToRawIntBits()- it will return a floating-point representation value according to IEEE floating-point "single format" bit layout preserving NaN values.
- floatValue()- it will return the float value of this Float object
- hashCode()- it will return a hash code for this Float object
- intBitsToFloat()- it will return the float value for the given bit representation
- intValue()- it will return an int value for this Float object
- isFinite()- it will return a 'true' value if the argument is a finite floating-point value
- isInfinite()- it will return a 'true' value if this float value or the specified number is infinitely large in magnitude
- isNan()- it will return a 'true' value if the specified number is NaN(Not a Nan) value.
- longValue()- it will return a long value for this float object
- max()- it will return the greatest of two float values
- min()- it will return the smallest of two float values
- parseFloat()- it will return a new float initialized to the value of the specified String
- shortValue()- it will return a short value for this Float object
- sum()- it will add the two float values together
- toHexString()- it will return a hexadecimal string representation of the float argument.
- toString()- it will return a string representation of the Float object
- valueOf()- it will return a Float instance representing the specified float or string value.
Example-
public class Simple {
public static void main(String[] args) {
Float fl1 = new Float(34) ;
Float fl2 = new Float(fl1/0);
System.out.println(fl2+" value for isInfinite() method is : " +Float.isInfinite(fl2));
System.out.println(fl1+" value for Finite() method : "+Float.isFinite(fl1));
int f3=fl1.intValue()*fl2.intValue();
System.out.println("intValue() method will return : "+fl1+"*"+fl2+" = "+f3 );
int f4 = fl1.hashCode();
System.out.println("Hash code value of "+fl1+ " is : "+f4);
}
}
Output-
7. Double class- this class will allow you to wrap the primitive type double into an object. The class object will contain only the double type of field. Below is the list of the methods that are used in the double class.
- byteValue()- It will return the Double value as a byte after the conversion.
- compare(double d1, double d2)- It will compare the two double values.
- compareTo(Double another Double)- It will compare the two Double objects on anumeric basis.
- DoubleToLongBits(double value)- It will return the floating point represented value that is provided by IEEE 754 "double format" bit layout.
- doubleToRawLongBits(double value)- It will return the floating point representation value provided by IEEE 754 "double format" bit layout.
- doubleValue()- It will return the Double value as a double after the conversion.
- equals(Object obj)-It will compare the object with the specified object.
- floatValue()-It will return the float type value for the provided Double object.
- hashCode()- It will return the hash code for the provided Double object.
- hashCode(Double value)- It will return the hash code for the specified Double value.
- intValue()-It will return the Double value as an int after the conversion.
- isFinite(double d)- It will return the true if the argument is a finite floating point. Otherwise, returns false.
- isInfinite()-It will return true if the double value’s magnitude is infinitely large Otherwise, returns false.
- isInfinite(double v)- It will return the true if the provided number’s magnitude is infinitely large. Otherwise, returns false.
- isNaN()- It will return the true if the double value is Not a Number value. Otherwise, returns false.
- IsNaN(double v)- It will return the true if the given number is Not a Number value. Otherwise, returns false.
- longBitsToDouble(long bits)- It will return the Double type value in correspondence to the given bit representation.
- longValue()-It will return the Double value as long after the conversion.
- max(double a, double b)-It will return the greater of the two double values.
- min(double a, double b)-It will return the smaller of the two double values.
- parseDouble(String s)- It will return a new double which is initialized by the value provided by the String.
- shortValue()-It will return the Double value as a short after the conversion.
- sum(double a, double b)- It will add the two values as per the + operator.
- toHexString(double d)- It will return a hexadecimal string that is represented by the double argument.
- toString()- It will return a string represented by the Double object.
- toString(double d)-It will return a string represented by the double argument.
- valueOf(double d)-It will return the Double object represented by a double value.
- valueOf(String s)- It will return the object of Double which holds the double value represented by string argument.
Example-
public class Simple {
public static void main(String[] args) {
Double d1 = 72d;
Double d2 = 91d;
Double d3 = Double.NaN;
Double d4 = Double.POSITIVE_INFINITY;
System.out.println("The hashCode for '"+d1+"' is given as :"+ d1.hashCode());
System.out.println("The interger type value '"+d2+"' is given as :"+d2.intValue());
System.out.println("Is the number '"+d3+"' is NaN? :"+d3.isNaN());
System.out.println("Is the number '"+d2+"' is finite? "+d4.isInfinite());
}
}
Output-